package com.tklab.smartcity.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tklab.smartcity.App;
import com.tklab.smartcity.R;
import com.tklab.smartcity.bean.NewsBean;
import com.tklab.smartcity.util.RetrofitUtil;


import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> {

    //数据源
    private List<NewsBean.RowsBean> list;

    public NewsAdapter(List<NewsBean.RowsBean> list){
        this.list=list;
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view=LayoutInflater.from(App.context).inflate(R.layout.home_news_adapter,null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        NewsBean.RowsBean r=list.get(position);
        holder.txtTitle.setText(r.getTitle());
        holder.txtContent.setText(r.getContent());

        String url= RetrofitUtil.BASE_URL+r.getCover();
        Glide.with(App.context).load(url).into(holder.imgNews);
    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        public ImageView imgNews;
        public TextView txtTitle;
        public TextView txtContent;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imgNews=itemView.findViewById(R.id.imgNews);
            txtTitle=itemView.findViewById(R.id.txtTitle);
            txtContent=itemView.findViewById(R.id.txtContent);
        }
    }
}
